//
//  HomeTableViewController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/6.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit
let LQHomeCellID = "homeTableViewCell"
class HomeTableViewController: VisitorViewController {

    private lazy var listModel:StatusListViewModel = StatusListViewModel()
    private lazy var photoBrowserAnimator:LQPhotoBrowserAnimator = LQPhotoBrowserAnimator()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !UserAccountViewModel.shareAccountModel.isLogin {
            visView?.setUpInfo(imagName:nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        //注册重用cell
        tableView.registerClass(HomeRetweetedCell.classForCoder(), forCellReuseIdentifier: HomeRetweetedCellID)
        tableView.registerClass(HomeNormalViewCell.classForCoder(), forCellReuseIdentifier: HomeNormalViewCellID)
        ///分割线
        tableView.separatorStyle = .None
        tableView.estimatedRowHeight = 150
        
        // 上拉刷新视图
        tableView.tableFooterView = pullupView
        
        refreshControl = WBrefreshController()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
        NSNotificationCenter.defaultCenter().addObserverForName(LQDidSelectStatusPictureNotification,
            object: nil,
            queue: nil) {
                [weak self] (notif) -> Void in
            print(notif)
                
                
                guard let index = notif.userInfo?[LQDidSelectStatusPictureIndexPathKey] as? NSIndexPath else {
                    return
                }
                guard let urls = notif.userInfo?[LQDidSelectStatusPictureURLsKey] as? [NSURL] else {
                    return
                }

                guard let statusPictureV = notif.object as? StatusPictureView else {
                    return
                }
            let photoBrowserView:LQPhotoBrowserViewController = LQPhotoBrowserViewController(indexPath: index, thumb_URLs: urls)
            
                
            photoBrowserView.modalPresentationStyle = UIModalPresentationStyle.Custom
            photoBrowserView.transitioningDelegate = self?.photoBrowserAnimator
                self?.photoBrowserAnimator.setPresentDelegate(statusPictureV, dismissDelegate: photoBrowserView, indexPath: index)
                
            self?.presentViewController(photoBrowserView, animated: true, completion: nil)
       
        }
        
        ///加载数据
        loadData ()
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    /// 上拉刷新提示视图
    private lazy var pullupView: UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        indicator.color = UIColor.lightGrayColor()
        
        return indicator
    }()
    
}



//加载微博数据
extension HomeTableViewController {
    
    func loadData(){

        self.refreshControl?.beginRefreshing()
        
        listModel.loadData(isPullup: pullupView.isAnimating()) { (isSuccess) -> () in

           self.refreshControl?.endRefreshing()
            // 关闭上拉刷新
            self.pullupView.stopAnimating()
            
            if !isSuccess {
                print("加载微博数据失败")
            }
            self.tableView.reloadData()
            
        }
    }
}

//tableView DateSource

extension HomeTableViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listModel.statusList.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //取出模型
        let viewModel = listModel.statusList[indexPath.row]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(viewModel.reuseID, forIndexPath: indexPath) as! HomeTableViewCell

        cell.viewModel = viewModel
        // 4. 判断是否是最后一条微博
        if indexPath.row == listModel.statusList.count - 1 && !pullupView.isAnimating() {
            // 开始动画
            pullupView.startAnimating()
            
            // 上拉刷新数据
            loadData()
            
            print("上拉刷新数据")
        }
        
        
        return cell
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return listModel.statusList[indexPath.row].rowHeight
    }
    
}

    

